package com.enrollandpay.service;

import com.enrollandpay.base.BaseService;
import com.enrollandpay.dao.ActivityMapper;
import com.enrollandpay.dao.SchoolMapper;
import com.enrollandpay.dao.SignupMapper;
import com.enrollandpay.dao.StudentMapper;
import com.enrollandpay.model.Activity;
import com.enrollandpay.model.Signup;
import com.enrollandpay.model.Student;
import com.enrollandpay.utils.CommonUtil;
import com.enrollandpay.utils.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by 94504 on 2017/6/28.
 */
@Service
public class SignupService extends BaseService<Signup, SignupMapper>
{
    @Autowired
    private SignupMapper signupMapper;
    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private StudentMapper studentMapper;


    /**
     * 根据signup的字段来查询signup，并设置自定义属性
     */
    public List<Signup> selectSignup(Signup signup)
    {
        List<Signup> resultList = null;

        if(signup != null)
        {
            resultList = signupMapper.selectSignup(signup);
            if(resultList != null)
            {
                Student student = null;
                Activity activity = null;

                // 设置自定义属性,用于页面显示
                setCustomAttributes(resultList);
            }

            return resultList;
        }
        else
        {
            resultList = signupMapper.selectAll();
            setCustomAttributes(resultList);

            return resultList;
        }

    }


    /**
     * 设置自定义属性
     * @param resultList
     */
    private void setCustomAttributes(List<Signup> resultList)
    {
        Student student = null;
        Activity activity = null;
        for(Signup aSignup : resultList )
        {
            Integer activityId = aSignup.getActivityId();
            student = studentMapper.selectByPrimaryKey(aSignup.getStudentId());
            activity = activityMapper.selectByPrimaryKey(aSignup.getActivityId());
            if(student != null)
                aSignup.setStudentName(student.getName());

            if(activity != null)
                aSignup.setActivityName(activity.getName());
        }

    }

}
